#!/bin/bash
#Install Mapnik library

#The Mapnik library is the first of two items sometimes called "Mapnik". The other item is a collection of tools that OpenStreetMap uses to invoke Mapnik.

#The official and up-to-date Mapnik Installation Instructions are here.

#You might find that this procedure works as well.

#Get some dependencies for building the Mapnik library.
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install subversion autoconf screen munin-node munin htop
sudo apt-get install libltdl3-dev libpng12-dev libtiff4-dev libicu-dev
sudo apt-get install libboost-python1.40-dev python-cairo-dev python-nose
sudo apt-get install libboost1.40-dev libboost-filesystem1.40-dev
sudo apt-get install libboost-iostreams1.40-dev libboost-regex1.40-dev libboost-thread1.40-dev
sudo apt-get install libboost-program-options1.40-dev libboost-python1.40-dev
sudo apt-get install libfreetype6-dev libcairo2-dev libcairomm-1.0-dev
sudo apt-get install libgeotiff-dev libtiff4 libtiff4-dev libtiffxx0c2
sudo apt-get install libsigc++-dev libsigc++0c2 libsigx-2.0-2 libsigx-2.0-dev
sudo apt-get install libgdal1-dev python-gdal
sudo apt-get install imagemagick ttf-dejavu

Build Mapnik library from source.
cd ~
mkdir src bin planet
cd ~/src
#This link does not wotk because of an update for mapnik2
#svn co http://svn.mapnik.org/tags/release-0.7.1/ mapnik
#We will use an archive of mapnik 0.7.1
wget https://github.com/mapnik/mapnik/tarball/release-0.7.1
tar xvzf mapnik-mapnik-release-0.7.1-0-gdb89f1c.tar.gz
sudo mv mapnik-mapnik-096fa08/ mapnik
cd mapnik
python scons/scons.py configure INPUT_PLUGINS=all OPTIMIZATION=3 SYSTEM_FONTS=/usr/share/fonts/truetype/
python scons/scons.py
sudo python scons/scons.py install
sudo ldconfig

#Confirm that Mapnik library is installed.
echo "Input 'import mapnik' after <<<<"
echo "If python replies with the second chevron prompt ">>>" and without errors, then Mapnik library was found by Python. Congratulations."
echo "To exit python, input 'enter()'"
python

#Install Mapnik tools

#The Mapnik tools are the second item sometimes called "mapnik". This is a collection of tools from OpenStreetMap for making effective use of the Mapnik library.

cd ~/bin
svn co http://svn.openstreetmap.org/applications/rendering/mapnik
#Install prepared world boundary data

#Mapnik uses prepared files to generate coastlines and ocean for small scale maps. This is faster than reading the entire database to render zoom levels from zero to nine.

#This section now includes the additional shape files that were added to OpenStreetMap default styles in mid-2010. Beware of the long, strange looking links with the repeated http. They are unlikely to copy / paste directly. Use copy link location or equivalent.

cd ~/bin/mapnik
mkdir world_boundaries
wget http://tile.openstreetmap.org/world_boundaries-spherical.tgz
tar xvzf world_boundaries-spherical.tgz
wget http://tile.openstreetmap.org/processed_p.tar.bz2
tar xvjf processed_p.tar.bz2 -C world_boundaries
wget http://tile.openstreetmap.org/shoreline_300.tar.bz2
tar xjf shoreline_300.tar.bz2 -C world_boundaries
wget http://www.naturalearthdata.com/http//www.naturalearthdata.com/download/10m/cultural/10m-populated-places.zip
unzip 10m-populated-places.zip -d world_boundaries
wget http://www.naturalearthdata.com/http//www.naturalearthdata.com/download/110m/cultural/110m-admin-0-boundary-lines.zip
unzip 110m-admin-0-boundary-lines.zip -d world_boundaries

#Render your first map

#The database is loaded and the tools are installed. Let's test everything together. Remember to replace "username" with your username.

cd ~/bin/mapnik
./generate_xml.py --host 82.67.139.156 --port 5432 --dbname osm --user serveurose --password ose
./generate_image.py
